//
//  HHInputPhoneNumbrtViewController.m
//  HHBroker
//
//  Created by He Wei on 9/17/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHInputPhoneNumbrtViewController.h"
#import "HttpRequestManager.h"
#import "NSString+ValidateArgument.h"
#import "HHWebViewController.h"
#import "HHInputAuthorCodeViewController.h"
#import "HHWelcomeViewController.h"

@interface HHInputPhoneNumbrtViewController () <UITextFieldDelegate, UIAlertViewDelegate>

@property (nonatomic, weak) IBOutlet UITextField *passwordTextField;
@property (nonatomic, weak) IBOutlet UIButton *visiblePasswordButton;
@property (nonatomic, strong) IBOutlet UILabel *label;
@property (nonatomic, strong) IBOutlet UITextField *phoneNumberTextField;
@property (nonatomic, strong) IBOutlet UIButton *getAuthorCodeButton;
@end

@implementation HHInputPhoneNumbrtViewController
{
    NSString *_phoneNumber;
    NSString *_authorCode;
    BOOL _getAuthorCodeButtonEnable;
    BOOL _isTruePhoneNumber;
    BOOL _isTruePassword;

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = NSLocalizedString(@"注册", @"");
    }
    return self;
}

#pragma mark - Life Cycle

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 以下为测试
}
#pragma mark - Http
- (void)getAuthCode
{
    [self.view endEditing:YES];
    __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    //网络请求
    //加入加密字段
    NSString *phone = self.phoneNumberTextField.text;
    NSString *encPhone = self.phoneNumberTextField.text;
    NSString *urlString = [NSString stringWithFormat:@"%@&mobile=%@", KUrlGetCode, encPhone];
    HHInputAuthorCodeViewController *authorCodeVC = [[HHInputAuthorCodeViewController alloc] init];
    authorCodeVC.readAuthorCodePhone = encPhone;
    [self.navigationController pushViewController:authorCodeVC animated:YES];
    return;

    [HttpRequestManager getWithUrlString:urlString success:^(id response)
    {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1)
        {
            //发送成功
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
            hub.mode = MBProgressHUDModeCustomView;
            hub.customView = imageView;
            hub.labelText = NSLocalizedString(@"发送成功，请查看手机短信", nil);;
            hub.completionBlock = ^
            {
                HHInputAuthorCodeViewController *authorCodeVC = [[HHInputAuthorCodeViewController alloc] init];
                authorCodeVC.readAuthorCodePhone = encPhone;
                [self.navigationController pushViewController:authorCodeVC animated:YES];
             };
        [hub hide:YES afterDelay:1];
        }
        else
        {
            NSString *msgString = [response objectForKey:@"msg"];
            if (code == -1)
            {
                [hub hide:NO];
                NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
                [ud setObject:phone forKey:kUDJYAccount];
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"该手机号已被注册，可使用该手机号直接登录", nil) message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:@"登陆", nil];
                [alertView show];
                alertView.tag = 101;
            }
            else
            {
                hub.mode = MBProgressHUDModeText;
                hub.labelText = msgString;
                [hub hide:YES afterDelay:1];

            }
        
        }
    
    } failure:^(NSError *error)
    {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题";
        [hub hide:YES afterDelay:2];
    
    } isLogin:NO];
    

}


#pragma mark - Delegate

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]) {
        if (_getAuthorCodeButtonEnable) {
            [self getAuthCodeButtonClicked:self.getAuthorCodeButton];
            [textField resignFirstResponder];
        }
        return NO;
    }
    return YES;
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 101) {
        if (buttonIndex == 1) {
            //login
            // reload account
            HHWelcomeViewController *welcome = self.navigationController.viewControllers[0];
            welcome.nextToGo = JYADLogin;
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
    else
    {
        if (buttonIndex == 1) {
           [self getAuthCode];
            

        }
    }

}

#pragma mark -
#pragma mark - Event Response

- (IBAction)textFieldValueChanged:(UITextField *)textField
{
    if (textField == self.phoneNumberTextField) {
        if (textField.text.length > 0) {
            _isTruePhoneNumber = YES;
            self.getAuthorCodeButton.enabled = YES;
        }
        else {
            _isTruePhoneNumber = NO;
            self.getAuthorCodeButton.enabled = NO;
        }
    }

}

- (IBAction)getAuthCodeButtonClicked:(UIButton *)sender
{
    [self.view endEditing:YES];
    NSString *phoneNumber = self.phoneNumberTextField.text;
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    // check phonenumber
    if (![NSString validateMobile:phoneNumber])
    {
        alertView.message = @"请填入正确的手机号";
        [alertView show];
        return;
    }
    // check whether agree with the paper or not
    if (!self.checkButton.selected)
    {
        alertView.message = @"您必须同意《app注册协议》才可以继续";
        [alertView show];
        return;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认手机号码" message:[NSString stringWithFormat:@"我们将发送短信到这个号码:%@", phoneNumber] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
    [alert show];
}

- (IBAction)changeCheckStatus:(UIButton *)sender
{
    if (sender.selected) {
        sender.selected = NO;
    }
    else{
        sender.selected = YES;
    }

}

- (IBAction)userPropertyButtonClick:(UIButton *)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"file_registerPrivacy" ofType:@"txt"];
    HHWebViewController *webV = [[HHWebViewController alloc] init];
    webV.fileString = path;
    webV.title = @"App注册协议";
    [self.navigationController pushViewController:webV animated:YES];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];

    
}


@end
